import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Order } from '../entities/order.entity'
import { In, Repository } from 'typeorm'
import { OrderDesc } from '../entities/orderDesc.entity'
import { Goods } from '../entities/goods.entity'
import { snowflake } from '../utils'

@Injectable()
export class OrderService {
  constructor(
    @InjectRepository(Order)
    private readonly orderRepository: Repository<Order>,
    @InjectRepository(OrderDesc)
    private readonly orderDescRepository: Repository<OrderDesc>,
    @InjectRepository(Goods)
    private readonly goodsRepository: Repository<Goods>,
  ) {}

  /**
   * 删除订单
   * @param id
   */
  async del(id) {
    // { id: oid }别名
    const { id: oid } = await this.orderRepository.softRemove({ id })
    return oid ? '删除成功' : '删除失败'
  }

  /**
   * 添加订单
   * @param uid
   * @param address
   * @param info
   */
  async save({ uid, address, info }) {
    // 添加order: id  totalPrice
    // 添加 orderDesc:oid title show  price
    // 生成订单 id
    const id = snowflake()
    // console.log(id)
    // console.log(info) // 数组对象
    // 订单的商品id
    const gids = info.map((item) => item.gid)
    let totalPrice = 0
    // 商品数据
    const goods = await this.goodsRepository.find({ where: { id: In(gids) } })
    goods.forEach((item, index) => {
      // 创建订单详情对象
      const orderDesc = new OrderDesc()
      orderDesc.oid = id
      orderDesc.title = item.title
      orderDesc.show = item.show
      orderDesc.price = item.price
      orderDesc.num = info[index].num
      // 计算总价
      totalPrice += orderDesc.price * orderDesc.num
      // 添加订单详情表
      this.orderDescRepository.save(orderDesc)
    })
    // console.log(totalPrice)
    // 添加订单表
    // const { id: oid } = await this.orderDescRepository.save({ id, totalPrice, address, uid }) // 错误写法
    const { id: oid } = await this.orderRepository.save({ id, totalPrice, address, uid })
    return oid ? '添加成功' : '添加失败'
  }

  /**
   * 查询订单
   * @param body
   */
  async queryAll(body) {
    // 查询出【用户】的x状态的订单
    const orders = await this.orderRepository.find(body)
    // console.log(orders)
    // 方法一
    // const o = []
    // for (const item of orders) {
    //   const orderDesc = await this.orderDescRepository.find({ where: { oid: item.id } })
    //   o.push({ ...item, orderDesc })
    // }
    // return o

    // 订单id数组
    const ids = orders.map((item) => item.id)
    // console.log(ids)
    // 根据订单id查询订单详情列表
    const orderDescs = await this.orderDescRepository.find({ where: { oid: In(ids) } })
    // console.log(orderDescs)
    // 方法二
    // const oderResult = []
    // // 返回组织结果
    // orders.forEach((order) => {
    //   oderResult.push({
    //     ...order,
    //     // 过滤
    //     orderDesc: orderDescs.filter((item) => item.oid === order.id),
    //   })
    // })
    // // console.log(oderResult)
    // return oderResult

    // 方法二【简化】
    return orders.map((order) => ({
      ...order,
      // 过滤
      orderDesc: orderDescs.filter((item) => item.oid === order.id),
    }))
  }

  /**
   * 支付
   * @param id
   */
  async pay(id) {
    // 支付代码...
    const { affected } = await this.orderRepository.update(id, { status: 1 })
    return affected ? '支付成功' : '支付失败'
  }

  /**
   * 取消订单
   * @param id
   */
  async cancel(id) {
    const { affected } = await this.orderRepository.update(id, { status: 2 })
    return affected ? '取消成功' : '取消失败'
  }
}
